package  src
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	/**
	 * ...
	 * @author Luiz Felipe de S. R.
	 */
	public class Level extends MovieClip
	{
		public var carro : Carro;
		var mapa : Mapa1;
		var player : Player;
		
		var miniMapa : Mapa1;
		var miniCarro : MiniCarro;
		
		var fase : uint;
		
		var fundoMiniMapa : Sprite;
		var tamanhoMiniMapa : Number;
		
		var inicioCorrida1 : InicioCorrida;
		var inicioCorrida1PX : Number;
		var inicioCorrida1PY : Number;
		
		var miniInicioCorrida1 : InicioCorrida;
		
		var velocimetro : Velocimetro;
		
		var cont : int = 0;
		var cont2 : int = 0;
		
		var inRace : Boolean = false;
		
		var mission : Mission = new Mission();
		
		var checkpointPX : Array;
		var checkpointPY : Array;
		
		var x1, x2 : Number;
		var topForce : Number;
		var a, b, c, xMed : Number;
		var xa, ya : Number;
		
		var getIn, getOut : Boolean;
		private var abrirPorta  : Boolean;
		
		
		public var numClick : int = 0;
		public function Level() 
		{
			fase = 1;
			tamanhoMiniMapa = 40;
			
			desenharTela();
			
			this.addEventListener(Event.ENTER_FRAME, update);
			
			x1 = -3600;
			x2 = 12400;
			topForce = 475;
			xMed = (x1 + x2) / 2;
			a = topForce / (xMed * xMed - (x1 + x2) * xMed + x1 * x2);
			b = -a * (x1 + x2);
			c = a * x1 * x2;
			xa = 6000;
			ya = a * (xa * xa) + b * xa + c;
			//trace(ya);
			//trace(xMed);
		}
		
		public function update(e:Event):void
		{
			//trace(carro.x, carro.y);
			camera();
			colisaoMapa();
			
			if (numClick == 1)
			{
				
				if (player.hitTestObject(carro))
				{
					if (!getIn)
					{
						carro.add();
						player.remove();
						getIn = true;
					}
					carro.mover();
					player.px = carro.x - mapa.x;
					player.py = carro.y - mapa.y;
					
					player.rotation = carro.rotation;
					velocimetro.visible = true;
					getOut = false;
					player.visible = false;
					
				/*	if (carro.sai)
					{
						player.visible = true;
					}*/
				
				abrirPorta = true;
				}
				else
				{
					numClick = 0;
				}
				
			}
			
			else if (numClick == 0)
			{
				if (!getOut)
				{
					player.add();
					carro.remove();
					getOut = true;
				}
				player.update();
				velocimetro.visible = false;
				carro.mover();
				getIn = false;
				player.visible = true;
				
				
				if (abrirPorta)
				{
					player.rotation -= 90;
					player.px = carro.x - mapa.x - Math.sin(player.rotation * (Math.PI / 180)) * (-13);
					player.py = carro.y - mapa.y - Math.cos(player.rotation * (Math.PI / 180)) * (13);
					//player.px += Math.sin((player.rotation + 90) * (Math.PI / 90)) * 4;
					//player.py += Math.cos((player.rotation + 90) * (Math.PI / 90)) * 4;
					
					abrirPorta = false;
				}
				
			}
			
			if (inRace == false)
			{
				colisaoInicioCorrida();
			}
			if (inRace == true)
			{
				corrida();
			}
			velocimetro.ponteiro.rotation = Math.abs(carro.velocidade) * 20 + 30;
		}
		
		public function desenharTela():void
		{
			if (fase == 1)
			{
				mapa = new Mapa1();
				//mapa.scaleX *= 5;
				//mapa.scaleY *= 5;
				mapa.x = 0;
				mapa.y = 0;
				
				fundoMiniMapa = new Sprite();
				fundoMiniMapa.graphics.lineStyle(1, 0x000000);
				fundoMiniMapa.graphics.beginFill(0x999999, 0.5);
				fundoMiniMapa.graphics.drawRoundRect(0, 0, (mapa.width / tamanhoMiniMapa) + 20, (mapa.height / tamanhoMiniMapa) + 20, 20);
				fundoMiniMapa.graphics.endFill();
				fundoMiniMapa.x = 10;
				fundoMiniMapa.y = 10;
				
				miniMapa = new Mapa1();
				miniMapa.width = mapa.width / tamanhoMiniMapa;
				miniMapa.height = mapa.height / tamanhoMiniMapa;
				miniMapa.alpha = 0.75;
				miniMapa.x = 20;
				miniMapa.y = 20;
				
				miniCarro = new MiniCarro();
				
				inicioCorrida1 = new InicioCorrida();
				inicioCorrida1.x = 2284;
				inicioCorrida1.y = 3703;
				inicioCorrida1.track = 1;
				inicioCorrida1PX = 2284;
				inicioCorrida1PY = 3703;
				
				miniInicioCorrida1 = new InicioCorrida;
				miniInicioCorrida1.width = 5;
				miniInicioCorrida1.height = 5;
				miniInicioCorrida1.x = (inicioCorrida1PX / tamanhoMiniMapa) + 20;
				miniInicioCorrida1.y =  (inicioCorrida1PY / tamanhoMiniMapa) + 20;
				
				velocimetro = new Velocimetro();
				velocimetro.x = Main._instance.stage.stageWidth - (velocimetro.width / 2) - 20;
				velocimetro.y = (velocimetro.height / 2) + 20;
				velocimetro.alpha = 0.5;
				
				carro = new Carro();
				//carro.x = Main._instance.stage.stageWidth / 2;
				//carro.y = Main._instance.stage.stageHeight / 2;
				carro.rotation = 180;
				carro.px = 2184;
				carro.py = 3503;
				
				player = new Player();
				player.x = Main._instance.stage.stageWidth / 2;
				player.y = Main._instance.stage.stageHeight / 2;
				player.px = 2184;
				player.py = 3403;
				
				this.addChild(mapa);
				this.addChild(fundoMiniMapa);
				this.addChild(miniMapa);
				this.addChild(miniCarro);
				this.addChild(miniInicioCorrida1);
				this.addChild(velocimetro);
				this.addChild(carro);
				this.addChild(player);
				mapa.addChild(inicioCorrida1);
				
				checkpointPX = new Array();
				checkpointPY = new Array();
				
				
			}
		}
		
		public function camera():void
		{
			mapa.x = -player.px + player.x;
			mapa.y = -player.py + player.y;
			
			//inicioCorrida1.x = -carro.px + carro.x + inicioCorrida1PX;
			//inicioCorrida1.y = -carro.py + carro.y + inicioCorrida1PY;
			
			carro.x = carro.px - player.px + player.x;
			carro.y = carro.py - player.py + player.y;
			
			if (inRace)
			{
				if (mission.checkpointList != null)
				{
					for (var j: int = 0; j < mission.checkpointList.length; j++)
					{
						mission.checkpointList[j].x = -player.px + player.x + checkpointPX[j];
						mission.checkpointList[j].y = -player.py + player.y + checkpointPY[j];
					}
				}
			}
			
			miniCarro.x = ((carro.x - mapa.x) / tamanhoMiniMapa) + 20;
			miniCarro.y = ((carro.y - mapa.y)/ tamanhoMiniMapa) + 20;
			miniCarro.rotation = carro.rotation;
		}
		
		public function colisaoMapa():void
		{
			if (!mapa.hitTestPoint(stage.stageWidth / 2, stage.stageHeight / 2, true))
			{
				if (carro.alpha > 0)
				{
					//carro.alpha -= 0.05;
				}
				
				carro.velocidade /= 1.03;
				
				/*if (carro.alpha <= 0)
				{
					
				}*/
			}
			else
			{
				if (carro.alpha < 1)
				{
					//carro.alpha += 0.05;
				}
			}
			
			if (mapa.areiaFofa.hitTestPoint(stage.stageWidth / 2, stage.stageHeight / 2, true))
			{
				carro.velocidade /= 1.05;
			}
		}
		
		public function colisaoInicioCorrida():void
		{
			if (carro.hitTestObject(inicioCorrida1) && carro.velocidade == 0)
			{
				cont++;
				if (cont >= 120)
				{
					inRace = true;
					cont = 0;
					inicioCorrida1.visible = false;
					miniInicioCorrida1.visible = false;
					
					mission.init();
					
					for (var i : int = 0; i < mission.checkpointList.length; i++)
					{
						checkpointPX.push(mission.checkpointList[i].x);
						checkpointPY.push(mission.checkpointList[i].y);
					}
					
					mission.checkpointList[0].visible = true;
					mission.checkpointList[1].visible = true;
				}
			}
			else
			{
				cont = 0;
			}
		}
		
		public function corrida():void
		{
			if (carro.hitTestObject(mission.checkpointList[cont2 % mission.checkpointList.length]))
			{
				mission.checkpointList[cont2 % mission.checkpointList.length].visible = false;
				mission.checkpointList[(cont2 + 2) % mission.checkpointList.length].visible = true;
				
				cont2++;
				
				if (cont2 >= mission.checkpointList.length * 1)
				{
					cont2 = 0;
					inRace = false;
					
					inicioCorrida1.visible = true;
					miniInicioCorrida1.visible = true;
					
					checkpointPX = [];
					checkpointPY = [];
					for (var i : int = 0; i < mission.checkpointList.length; i++)
					{
						removeChild(mission.checkpointList[i]);
						mission.checkpoint = null;
						break;
					}
					mission = new Mission();
				}
			}
		}
		
	}

}